package elements.axis; 
import flash.text.TextField;

class YAxisLabelsLeft extends YAxisLabelsBase {

	

	public function new( parent:YAxisLeft, json:Dynamic ) {
		
		var values:Array<Dynamic>;
		var ok:Bool = false;
		var labelText:String = "#val#";
		
		if( json.y_axis )
		{
			if( Std.is( json.y_axis.labels, Array<String>) )
			{
				values = [];
				
				// use passed in min if provided else zero
				var i:Int = (json.y_axis && json.y_axis.min) ? json.y_axis.min : 0;
				for ( s in json.y_axis.labels )
				{
					values.push( { val:s, pos:i } );
					i++;
				}
				
				//
				// alter the MinMax Dynamic:
				//
				// use passed in max if provided else the number of values less 1
				i = (json.y_axis && json.y_axis.max) ? json.y_axis.max : values.length - 1;
				parent.set_y_max( i );
				ok = true;
			}
			else if ( Std.is( json.y_axis.labels, Dynamic) ) 
			{
				if ( Std.is( json.y_axis.labels.text, String) ) labelText = json.y_axis.labels.text;

				if ( Std.is( json.y_axis.labels.labels, Array<String>) )
				{
					values = [];
					for ( obj in json.y_axis.labels.labels )
					{
						if (Std.is( obj, Float)) 
						{
							values.push( { val:labelText, pos:obj } );
							i = (obj > i) ? cast( obj, Float) : i;
						}
						else if (Std.is( obj.y, Float))
						{
							s = (Std.is( obj.text, String)) ? obj.text : labelText;
							var labelStyle:Dynamic = { val:s, pos:obj.y }
							if (obj.colour != null)
								labelStyle.colour = obj.colour;
								
							if (obj.size != null)
								labelStyle.size = obj.size;
								
							if (obj.rotate != null)
								labelStyle.rotate = obj.rotate;
								
							values.push( labelStyle );
							i = (obj.y > i) ? obj.y : i;
						}
					}
					ok = true;
				}
			}				
		}
		
		if( !ok )
		{
			values = this.make_labels( parent.style.min, parent.style.max, false, parent.style.steps, labelText );
		}
		
		
		super(values,1,json,'y_label_','y_axis');
	}

	// move y axis labels to the correct x pos
	public override function resize( left:Float, sc:ScreenCoords ):Void {
		var maxWidth:Int = this.get_width();
		var i:Float;
		var tf:YTextField;
		
		for( i in 0...this.numChildren) {
			// right align
			tf = cast( this.getChildAt(i), YTextField);
			tf.x = left - tf.width + maxWidth;
		}
		
		// now move it to the correct Y, vertical center align
		for ( i in 0...this.numChildren) {
			tf = cast( this.getChildAt(i), YTextField);
			if (tf.rotation != 0) {
				tf.y = sc.get_y_from_val( tf.y_val, false ) + (tf.height / 2);
			}
			else {
				tf.y = sc.get_y_from_val( tf.y_val, false ) - (tf.height / 2);
			}
			
			//
			// this is a hack so if the top
			// label is off the screen (no chart title or key set)
			// then move it down a little.
			//
			if (tf.y < 0 && sc.top == 0) // Tried setting tf.height but that didnt work 
				tf.y = (tf.rotation != 0) ? tf.height : tf.textHeight - tf.height;
		}
	}
}
